

import request from "../../request/server.ts";

import axios from 'axios';

let cancelToken = axios.CancelToken;
//@ts-ignore
let cancel;


export  interface  Result<T>{
    code:string,
    data:T,
    msg:string
}


/**
 * 小文文件直接上传
 * @param data
 */
export function uploadFileApi(data:any,callbackUpload:unknown):Promise<Result<Array<number>>>{
    return  request({
        method:"post",
        url:"/file/upload_file",
        headers:{'Content-Type' : 'multipart/form-data'},
        data,

        onUploadProgress: e => {
            //@ts-ignore
            callbackUpload && callbackUpload(e)
        },
    })
}

/**
 * 创建文件夹
 * @param data
 */

export  interface  FolderType{
    folderId?:string,
      folderName?: string,
      parentId?: string
}
export  function  mkidrFolderApi(data:FolderType):Promise<Result<any>>{

    return  request({
        method:"post",
        url:"/file/mkdir_file",
        data
    })
}


/**
 * 查询文件数据
 * @param data
 */

export  function  findFileDatasApi(data:FolderType):Promise<Result<any>>{
    return  request({
        method:"post",
        url:"/file/find_file_data",
        data
    })

}


/**
 * 查询文件夹信息
 * @param data
 */

export  interface folderInfoType{
    folderId: string,
    folderName: string,
    parentID: string,
    uploadTime: string
}

export  function  findFolderInfosApi(data:string):Promise<Result<folderInfoType>>{
    return  request({
        method:"get",
        url:"/file/find_folder_info/"+data,

    })

}


/**
 * 删除文件
 * @param data
 */

export  interface  deleteParamstype{
     fileMd5:string,
      fileName:string,

}
export  function  deleteFileApi(data:deleteParamstype):Promise<Result<any>>{
    return  request({
        method:"delete",
        url:"/file/delete_file",
        data
    })
}

/**
 * 删除文件夹
 * @param data
 */

export  interface  deleteFolderType{
    currentFolderId: string,
    folderParentId: string
}

export  function  deleteFolderApi(data:deleteFolderType):Promise<Result<any>>{
    return  request({
        method:"delete",
        url:"/file/delete_folder",
        data
    })
}


/**
 * 检查文件上传状态
 */


export interface  checkFileParam{
    fileMd5: string,
    fileName: string,
    folderId: string,

}

export  function  checkFileUploadStatusApi(data: checkFileParam):Promise<Result<any>>{
    return  request({
        method:"post",
        url:"/file/check_file_upload_status",
        data
    })
}


export interface  shareFileParamType{
    loseData: number,
    shareFileType: number,
    shareId: string,
    sharePassword: string
}

export function shareFileApi(data:shareFileParamType):Promise<Result<string>>{
    return  request({
        method:"post",
        url:"/share/file/add",
        data
    })
}


export interface  reShareFileType{
    shareUuid: string,
    shareUserId: string,
    "shareUserName": string,
    shareDate: number,
    shareCreateTime: string,
    shareLoseTime: string,
    shareFileId: string,
    shareFileType: 0,
    sharePassword: string,
    sharePath: string,
    lose:boolean
}

export function  getShareFileApi():Promise<Result<Array<reShareFileType>>>{
    return  request({
        method:"get",
        url:"/share/file/findShareFile",

    })
}

//查询回收站文件
export interface  checkRecycleType{
    fileId: string,
    userId: string,
    fileUploadTime: string,
    folderId: string,
    fileName: string,
    filePath: string,
    fileType: number

}

export function  findRecyclebinFileApi():Promise<Result<Array<checkRecycleType>>>{
    return  request({
        method:"post",
        url:"/recycle/findRecycleFile",

    })
}

//删除回收站文件
export interface deleteRecycleType{
    fileId:string,

}

export function deleteRecyclebinFileApi(data:any):Promise<Result<any>>{
    return  request({
        method:"post",
        url:"/recycle/DelRecycleFile",
        data
    })
}

//恢复回收站文件
export interface  recoverRecycleType{
    fileId:string,

}

export function  recoverRecyclebinFileApi(data:any):Promise<Result<any>>{
    return  request({
        method:"post",
        url:"/recycle/ReRecycleFile",
        data
    })
}





export function axiosCancel() {
    //@ts-ignore
    cancel();
}

//获取已经上传的分片文件数组

export function  findChunkListApi(data:any):Promise<Result<any>>{
    return  request({
        method:"post",
        url:"/file/findChunkList",
        headers:{'Content-Type' : 'multipart/form-data'},
        data
    })
}
/**
 * 上传分片文件
 * @param data
 */
export function  uploadFileChuunkApi(data:any):Promise<Result<any>>{
    return  request({
        method:"post",
        url:"/file/uploadFileChunk",
        headers:{'Content-Type' : 'multipart/form-data'},
        cancelToken: new cancelToken(function executor(c) {
            // executor 函数接收一个 cancel 函数作为参数
            cancel = c;
        }),
        data

    })
}



export function  mergerFileFileApi(data:{fileMd5: string,
    fileName: string,
    fileSuffix: string}):Promise<Result<any>>{
    return  request({
        method:"post",
        url:"/file/merger",
        data
    })
}

//文件分类

export function  classFilesApi(data:any):Promise<Result<any>>{
    return  request({
        method:"get",
        url:"/search/searchFiles/"+data,

    })
}

//查询文件、文件夹信息
export function  searchFilesApi(data:any):Promise<Result<any>>{
    return  request({
        method:"post",
        url:"/search/search_file_floder_data/"+data,

    })
}